import config from '../../utils/config.js'
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var wxApi = require('../../utils/wxApi.js')
var wxRequest = require('../../utils/wxRequest.js');
var app = getApp();

Page({
    data: {
        carType: 1,
        carTypeName: '五座轿车或者SUV',
        group: [],
        listMain: [{
                name: "套餐一",
                front: 'JK72',
                end: 'JK35',
                price: 4080
            },
            {
                name: "套餐二",
                front: 'JK72',
                end: 'JK15',
                price: 4080
            },
            {
                name: "套餐三",
                front: 'JK72',
                end: 'NY36',
                price: 3600
            },
            {
                name: "套餐四",
                front: 'JK72',
                end: 'NY16',
                price: 3600
            },
            {
                name: "套餐五",
                front: 'JK72',
                end: 'NL35',
                price: 2980
            },
            {
                name: "套餐六",
                front: 'JK72',
                end: 'NL15',
                price: 2980
            },
            {
                name: "套餐七",
                front: 'JK76',
                end: 'NY36',
                price: 2280
            },
            {
                name: "套餐八",
                front: 'JK76',
                end: 'NY16',
                price: 2280
            },
            {
                name: "套餐九",
                front: 'JK76',
                end: 'NY35',
                price: 2280
            },
            {
                name: "套餐十",
                front: 'JK76',
                end: 'NL15',
                price: 2280
            },
            {
                name: "套餐十一",
                front: 'NL78',
                end: 'NL35',
                price: 2980
            },
            {
                name: "套餐十二",
                front: 'NL78',
                end: 'NL15',
                price: 2980
            },
            {
                name: "套餐十三",
                front: 'NL78',
                end: 'NY36',
                price: 2280
            },
            {
                name: "套餐十四",
                front: 'NL78',
                end: 'NY16',
                price: 2280
            }

        ],
        isAllChecked: false,
        isIndeterminate: false,
        switchChecked: false,
        totalPrice: 0,
        qd_price: 0,
        chd_price: 0,
        zqtz_price: 0,
        zhtz_price: 0,
        yqtz_price: 0,
        yhtz_price: 0,
        hd_price: 0,
        tc_price:0,

        multiIndexqd: [0, 0], // 默认值
        multiIndexchd: [0, 0], // 默认值
        multiIndexzqtz: [0, 0], // 默认值
        multiIndexzhtz: [0, 0], // 默认值
        multiIndexyqtz: [0, 0], // 默认值
        multiIndexyhtz: [0, 0], // 默认值
        multiIndexhd: [0, 0], // 默认值

        multiArrayqd: [],
        multiArraychd: [],
        multiArrayzqtz: [],
        multiArrayzhtz: [],
        multiArrayyqtz: [],
        multiArrayyhtz: [],
        multiArrayhd: [], 
        classArray: [
            ['1班a', '2班a', '3班a'], // 一年级的班级
            ['1班b', '2班b', '3班b'] // 二年级的班级  
        ],
        multiIndextc: [0,0,0], // 天窗是三级的 
        multiArraytc: [],
        windowArray:[],
        detailArray: []
    },
    extractContent(str) {
        var start = str.indexOf('(');
        var end = str.indexOf(')');
        if (start !== -1 && end !== -1) {
            return str.substring(start + 1, end);
        } else {
            return '';
        }
    },
    // 点击确认时触发
    bindMultiPickerChange(event) {

        var type = event.currentTarget.dataset.type;
        var that = this;
        let model = that.data.classArray[event.detail.value[0]];
        let selectd = model[event.detail.value[1]];
        let selectd_type = this.extractContent(selectd);
        //截取 内容
        //如果前挡 不等于 就提示类型错了
        let all = that.data.detailArray;
        let select_key = type + '_price';
        let flag = false;
        let totalPrice = that.data.totalPrice;
        let before_price = that.data[select_key];

        let multiIndex = 'multiIndex' + type;
        all.forEach(element => {
            if (selectd == element.model) {
                if (element[select_key] && ((type == 'qd' && selectd_type =='前挡') || (selectd_type =='侧后挡') )) {
                    //把之前的钱清除掉 
                    that.setData({
                        totalPrice: Number(totalPrice) + Number(element[select_key]) - Number(before_price),
                        [select_key]: element[select_key]
                    })
                } else {
                    flag = true;

                    
                }
                //break;  
            }
        });
        if (selectd == '请选择' || flag) {

            wx.showToast({
                icon: 'none',
                title: '前挡后挡不通用，请选择适用窗膜！',
                duration: 1000
            });
            that.setData({
                totalPrice: Number(totalPrice) - Number(before_price),
                [select_key]: 0,
                // [multiIndex] : [0,0] //选错了是否退回默认请选择
            })
        }

    },
    // 列改变时触发
    bindMultiPickerColumnChange(event) {
        var type = event.currentTarget.dataset.type;
        let multiIndex = 'multiIndex' + type
        let multiArray = 'multiArray' + type 
        const data = {
            [multiArray]: this.data[multiArray],
            [multiIndex]: this.data[multiIndex]
        }
        //看是哪个部件点过来的 重新整理数据
        // 获取滚动的是哪一列
        data[multiIndex][event.detail.column] = event.detail.value
        // 遍历 classArray
        this.data.classArray.forEach((item, index) => {
            // 滚动第一列
            if (event.detail.column === 0) {
                if (data[multiIndex][0] === index) {
                    data[multiArray][1] = item
                }
                data[multiIndex][1] = 0
            }
            this.setData(data)
        })
    },

    //三级窗户 
    // 点击确认时触发
    bindMultiPickerChangeWindow(event) {
        
        var type = event.currentTarget.dataset.type;
        var that = this;
        console.log(that.data.windowArray);
        console.log(event.detail);
        let window = that.data.windowArray[event.detail.value[0]];
        // console.log(window);
        let selectd = window[event.detail.value[2]];
        //console.log(selectd);   
        let tc_price = that.extractContent(selectd);
        //截取 内容  
        let select_key = type + '_price';
        let totalPrice = that.data.totalPrice;
        let before_price = that.data[select_key];
        console.log(tc_price);
        if (tc_price) {
            //把之前的钱清除掉 
            that.setData({
                totalPrice: Number(totalPrice) + Number(tc_price) - Number(before_price),
                [select_key]: tc_price
            })
        }else{
            that.setData({
                totalPrice: Number(totalPrice) - Number(before_price),
                [select_key]: 0
            })
        }
    

    },
    // 列改变时触发
    bindMultiPickerColumnChangeWindow(event) {
        var type = event.currentTarget.dataset.type;
        let multiIndex = 'multiIndex' + type
        let multiArray = 'multiArray' + type 
        const data = {
            [multiArray]: this.data[multiArray],
            [multiIndex]: this.data[multiIndex]
        }
        //看是哪个部件点过来的 重新整理数据
        // 获取滚动的是哪一列
        data[multiIndex][event.detail.column] = event.detail.value
        // 遍历 windowArray
        // console.log(this.data.windowArray);
        // console.log(event);
        this.data.classArray.forEach((item, index) => {
            // 滚动第一列
            if (event.detail.column === 0) { 
                if (data[multiIndex][0] === index) {
                    data[multiArray][1] = item
                }
                data[multiIndex][1] = 0
            } 
            //滑动中间那个
            if (event.detail.column === 1) { 
                data[multiIndex][1] = 0
            } 

            this.data.windowArray.forEach((item_son,index_son) =>{
                if (data[multiIndex][0] === index_son) {
                    data[multiArray][2] = item_son
                }
                data[multiIndex][1] = 0
              //  data[multiIndex][2] = 0
            }) 
            this.setData(data)
        })
    },
    onLoad(options) {
        var that = this;
        let url_param = options;
        let carType = 1;
        let carTypeName = '五座轿车或者SUV';
        if (url_param.index == 2) {
            carType = url_param.index;
            carTypeName = '六座及以上SUV或MPV';
        }
        that.setData({
            carType: carType,
            carTypeName: carTypeName
        });
        var window = wxRequest.getRequest(Api.getTotalPriceWindow());
        window.then(res => {
            var res = res.data.data;
        
            that.setData({
                detailArray: res.all,
                multiArrayqd: [res.series_name, res.model[0]],
                multiArraychd: [res.series_name, res.model[0]],
                multiArrayzqtz: [res.series_name, res.model[0]],
                multiArrayyqtz: [res.series_name, res.model[0]],
                multiArrayzhtz: [res.series_name, res.model[0]], 
                multiArrayyhtz: [res.series_name, res.model[0]],
                multiArrayhd: [res.series_name, res.model[0]],
                multiArraytc: [res.series_name, res.model[0], res.window[0] ],
 
                classArray: res.model,
                windowArray:res.window
            });
        });

    },
    onReady() {

    },
    switchChange(e) {
        //把对比他的金额全清除掉。
        var that = this ;
        that.setData({
            switchChecked: e.detail,
        });
        //如果打开适合 侧后挡是开的，那么所有的金额都打开
        let current_data = that.data;
        //除了前挡都恢复默认
        that.setData({
            totalPrice:Number(current_data.qd_price) + Number(current_data.tc_price),

            chd_price: 0,
            zqtz_price: 0,
            zhtz_price: 0,
            yqtz_price: 0,
            yhtz_price: 0,
            hd_price: 0,
            multiIndexchd: [0, 0], // 默认值
            multiIndexzqtz: [0, 0], // 默认值
            multiIndexzhtz: [0, 0], // 默认值
            multiIndexyqtz: [0, 0], // 默认值 
            multiIndexyhtz: [0, 0], // 默认值
            multiIndexhd: [0, 0], // 默认值
            
        })
        

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})